<?php 

function sndvivill_add_party(){
	return drupal_get_form('sndvivill_party_form');
}

function sndvivill_edit_party($party_id){
	//drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t('Vi vill'), 'vivill'), l(t('Party'), 'vivill/party'), t('Edit')));
	//menu_set_active_item('vivill/party');
	
	if(is_numeric($party_id) ){
		return drupal_get_form('sndvivill_party_form');
	}else{
		return t('error').": $party_id ".t('is not valid');
	}
}

function sndvivill_party_form_submit($form, &$form_state){
	db_set_active(VIVILL_DB);
	
	if(is_numeric($form_state['values']['party_id'])){
		db_query("UPDATE party SET label = '%s', name_sv = '%s', info = '%s' WHERE party_id = %d", $form_state['values']['label'], 
																								   $form_state['values']['name_sv'],
																								   $form_state['values']['info'],
																								   $form_state['values']['party_id']
																								   );
		db_set_active('default');
		drupal_set_message(t('Updated').' '.$form_state['values']['name_sv']);
	}else{
		db_query("INSERT INTO party (label, name_sv, info) VALUES('%s', '%s', '%s')", $form_state['values']['label'], $form_state['values']['name_sv'],$form_state['values']['info']);
		db_set_active('default');
		drupal_set_message(t('Added').' '.$form_state['values']['name_sv']);
	}
	
	
}

function sndvivill_party_form(){
	db_set_active(VIVILL_DB);
	
	$default = array();
	if(is_numeric(arg(2))){
		$result = db_query("SELECT * FROM party WHERE party_id = %d", arg(2));	
		$default = db_fetch_array($result);	
	}
	
	db_set_active('default');
	
	$form = array();
	$form['label'] = array(
	  '#weight' => '1',
	  '#size' => '50',
	  '#required' => '1',
	  '#title' => t('Label'),
	  '#type' => 'textfield',
	  '#default_value' => $default['label'],
	);
	$form['name_sv'] = array(
	  '#weight' => '2',
	  '#required' => '1',
	  '#type' => 'textfield',
	  '#title' => 'Year',
	  '#default_value' => $default['name_sv'],
	);

	$form['info'] = array(
	  '#required' => '1',
	  '#weight' => '3',
	  '#title' => 'Info sv',
	  '#type' => 'textarea',
	   '#default_value' => $default['info'],
	);

	$form['party_id'] = array(
		'#type' => 'hidden',
		'#title' => t('Id of the party'),
		'#maxlength' => 256,
		'#required' => FALSE,
		'#weight' => 10,
		'#default_value' => $default['party_id'],
	);		

	$form['submit'] = array(
		'#weight' => '4',
		'#type' => 'submit',
		'#value' => t('Save'),
	);		

	return $form;	
}


?>